package one.chchy.dp_demo.ui.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.snackbar.Snackbar;

import java.util.stream.Collectors;

import one.chchy.dp_demo.R;
import one.chchy.dp_demo.databinding.FragmentHomeBinding;
import one.chchy.dp_demo.enums.DeviceType;
import one.chchy.dp_demo.enums.ObjectDetect;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    private ObjectDetect detectMethod = ObjectDetect.yolov7;
    private DeviceType deviceType = DeviceType.Image;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

//        final TextView textView = binding.textHome;
//        homeViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        homeViewModel.getObjectDetect().observe(getViewLifecycleOwner(), objectDetects -> {
            if (objectDetects != null) {
                // 创建ArrayAdapter
                ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, objectDetects.stream().map(ObjectDetect::getName).collect(Collectors.toList()));
                binding.spinner.setAdapter(adapter);
            }
        });

        binding.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ObjectDetect objectDetect = ObjectDetect.get(id);
                detectMethod = objectDetect;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        homeViewModel.getDeviceType().observe(getViewLifecycleOwner(), deviceType -> {
            if (deviceType!= null) {
                ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_dropdown_item, deviceType.stream().map(DeviceType::getName).collect(Collectors.toList()));
                binding.detectObjectSpinner.setAdapter(adapter);
            }
        });
        binding.detectObjectSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                deviceType = DeviceType.get(id);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        binding.buttonExecute.setOnClickListener(v -> {
            Snackbar.make(v, String.format("select detect method: %s, detect object: %s", deviceType.Name, detectMethod.getName()), Snackbar.LENGTH_LONG)
                    .setAction("Action", null)
                    .setAnchorView(R.id.fab).show();
        });

        binding.imageView.setOnClickListener(v -> showImagePickDialog());
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    private void showImagePickDialog() {
        // 创建选择对话框
        String[] options = {"从相册选择", "从文件管理器选择"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("选择图片来源");
        builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {
                    // 从相册选择
                    pickImageFromGallery();
                } else {
                    // 从文件管理器选择
                    pickImageFromFileManager();
                }
            }
        });
        builder.show();
    }

    private void pickImageFromGallery() {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, IMAGE_PICK_GALLERY_CODE);
    }

    private void pickImageFromFileManager() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, IMAGE_PICK_FILE_CODE);
    }
}